package com.jxs.android.weixi;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.jxs.android.util.KeyboardUtil;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.TopBar;
import com.telecom.weixi.R;

public class MyCustomer_Customer extends LoadingMoreAbstractList {
	private CstInfoAdapter adapter;
	private String conditionCustName = "";

	private ImageButton filter_submit;
	private EditText filter_name;

	@Override
	public int getLayout() {
		return R.layout.bu_my_customer_customer_list;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		super.onCreate(savedInstanceState);
		filter_submit = (ImageButton) findViewById(R.id.filter_submit);
		filter_name = (EditText) findViewById(R.id.filter_name);
		filter_submit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				currentPage = 1;
				conditionCustName = filter_name.getText().toString();
				if (KeyboardUtil.isImeShow(MyCustomer_Customer.this)) {
					KeyboardUtil.hideIme(MyCustomer_Customer.this);
				}
				loadingData(currentPage, true);
			}
		});
		filter_name.clearFocus();
	}

	@Override
	public void loadingData(int currentPage, boolean showDialog) {
		getWebServiceData(WeixiApplication.getUserInfo().getTicket(), Which.MY_CUSTOMER_CUSTOMER, showDialog, new DataInitListeren() {
			@Override
			public void initData(BuLoadingDailog progressDialog, boolean isShow) {
				progressDialog.dismiss();
			}
		}, conditionCustName, PAGE_SIZE, currentPage);
	}

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new CstInfoAdapter(context);
		return adapter;
	}

	@Override
	public Object paserData(SoapObject retMsgObject, Object... params) {
		List<CstInfo> tems = new ArrayList<MyCustomer_Customer.CstInfo>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				CstInfo cstInfo = new CstInfo();
				if (taskBeanObject.hasProperty("cust_id")) {
					cstInfo.cust_id = taskBeanObject.getPropertySafelyAsString("cust_id");// 客户名称
				}
				cstInfo.cust_name = taskBeanObject.getPropertySafelyAsString("cust_name");// 客户名称
				cstInfo.cust_type_name = taskBeanObject.getPropertySafelyAsString("cust_type_name");// 客户类型
				cstInfo.custarpu_val = taskBeanObject.getPropertySafelyAsString("custarpu_val");// 客户ARPU
				cstInfo.product_info = taskBeanObject.getPropertySafelyAsString("product_info");// 产品信息
				cstInfo.income_info = taskBeanObject.getPropertySafelyAsString("income_info");// 收入信息
				tems.add(cstInfo);
			}
		}
		return tems;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateList(Message msg) {
		List<CstInfo> tems = (List<CstInfo>) msg.obj;
		empytText.setVisibility(View.GONE);
		loadMoreView.setVisibility(View.VISIBLE);
		if (currentPage == 1) {
			if (tems.size() < 1) {
				empytText.setVisibility(View.VISIBLE);
				loadMoreView.setVisibility(View.GONE);
			}
		}
		adapter.setCstInfos(tems);
		adapter.notifyDataSetChanged();
		if (tems.size() < PAGE_SIZE) {
			loadMoreButton.setText("数据已全部获取完毕");
			loadMoreButton.setEnabled(false);
		} else {
			loadMoreButton.setText("查看更多...");
			currentPage++;
		}
	}

	class CstInfoAdapter extends BaseAdapter {

		List<CstInfo> custInfos = new ArrayList<CstInfo>();

		private LayoutInflater inflater = null;
		private ViewHolder holder = null;

		public CstInfoAdapter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void setCstInfos(List<CstInfo> custInfos) {
			this.custInfos.addAll(custInfos);
		}

		@Override
		public int getCount() {
			return custInfos.size();
		}

		@Override
		public CstInfo getItem(int position) {
			return custInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final CstInfo custInfo = custInfos.get(position);

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.bu_list_item_cst_cst, null);
				holder = new ViewHolder();
				holder.position = (TextView) convertView.findViewById(R.id.position);
				holder.cust_name = (TextView) convertView.findViewById(R.id.cust_name);
				holder.cust_type_name = (TextView) convertView.findViewById(R.id.cust_type_name);
				holder.custarpu_val = (TextView) convertView.findViewById(R.id.custarpu_val);
				holder.product_info = (TextView) convertView.findViewById(R.id.product_info);
				holder.income_info = (TextView) convertView.findViewById(R.id.income_info);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.position.setText("No." + (position + 1));
			holder.cust_name.setText(custInfo.cust_name);
			holder.cust_type_name.setText(custInfo.cust_type_name);
			holder.custarpu_val.setText(custInfo.custarpu_val);
			holder.product_info.setText(custInfo.product_info);
			holder.income_info.setText(custInfo.income_info);

			convertView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(MyCustomer_Customer.this, MyCustomer_Customer_View.class);
					Bundle bundle = new Bundle();
					bundle.putString("cust_id", custInfo.cust_id);
					intent.putExtras(bundle);
					startActivity(intent);
				}
			});
			return convertView;
		}

		class ViewHolder {
			TextView position;// 序号
			TextView cust_name;// 客户名称
			TextView cust_type_name;// 客户类型
			TextView custarpu_val;// 客户ARPU
			TextView product_info;// 产品信息
			TextView income_info;// 收入信息
		}

	}

	class CstInfo {
		String cust_name = "客户名称";// 客户名称
		String cust_type_name;// 客户类型
		String cust_id = "0";
		String custarpu_val;// 客户ARPU
		String product_info;// 产品信息
		String income_info;// 收入信息
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarLogic(topBar);
		topBar.hindBtn();
		topBarReturn(topBar);
		topBar.setTitle(title);
		String cust_all_num = "";
		if (null != getIntent() && null != getIntent().getExtras()) {
			cust_all_num = getIntent().getExtras().getString("cust_all_num");
		}
		((TextView) findViewById(R.id.top_total_bar)).setText("我的客户数(" + cust_all_num + ")");
		findViewById(R.id.top_total_bar).setVisibility(View.VISIBLE);
	}

	@Override
	public String getMethod() {
		return "getCustBasePage";
	}

}
